package cn.lg.soar.system.auth.service.impl;

import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.current.CurrentProxy;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.system.auth.entity.Post;
import cn.lg.soar.system.auth.entity.PostRole;
import cn.lg.soar.system.auth.entity.UserOrg;
import cn.lg.soar.system.auth.mapper.PostMapper;
import cn.lg.soar.system.auth.model.PostDto;
import cn.lg.soar.system.auth.service.IPostService;
import cn.lg.soar.system.auth.service.PostRoleService;
import cn.lg.soar.system.auth.service.UserOrgService;
import cn.lg.soar.system.config.AuthCacheKey;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @author luguoxiang
 * @date 2021/8/21
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class PostServiceImpl extends BaseServiceImpl<PostMapper, Post> implements IPostService, CurrentProxy<IPostService> {

    @Autowired
    private PostRoleService postRoleService;
    @Autowired
    private UserOrgService userOrgService;

    @Cacheable(value = AuthCacheKey.POST_BY_ID, key = "#id")
    @Override
    public Post getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Long create(PostDto data) {
        Post convert = data.toPost();
        AssertUtil.isTrue(this.save(convert), "创建组织机构失败");
        if (!CollectionUtils.isEmpty(data.getRoleIds())) {
            postRoleService.setPostRole(convert.getId(), data.getRoleIds());
        }
        return convert.getId();
    }

    @CacheEvict(value = { AuthCacheKey.POST_BY_ID }, key = "#entity.id")
    @Override
    public boolean updateById(Post entity) {
        return super.updateById(entity);
    }

    @CacheEvict(value = { AuthCacheKey.POST_BY_ID }, key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(value = { AuthCacheKey.POST_BY_ID }, allEntries = true)
    @Override
    public void clearCache() {}

    @CacheEvict(value = { AuthCacheKey.POST_BY_ID }, allEntries = true)
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean removeByIds(Collection<?> list) {
        userOrgService.remove(Wrappers.<UserOrg>lambdaQuery().in(UserOrg::getPostId, list));
        postRoleService.remove(Wrappers.<PostRole>lambdaQuery().in(PostRole::getPostId, list));
        return super.removeByIds(list);
    }

    @Override
    public List<Post> listByUserId(Long userId) {
        List<Long> postIds = userOrgService.getPostIds(userId);
        return listByIds(postIds, AuthCacheKey.POST_BY_ID);
    }
}
